package com.cction.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.cction.R
import com.cction.activity.reinterest.CctionNewActivity
import com.cction.activity.house.HouseActivity
import com.cction.activity.tax.TaxActivity
import com.cction.comm.GridDividerItemDecoration
import com.cction.entry.ItemBean
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import kotlinx.android.synthetic.main.cction_main.*
import kotlinx.android.synthetic.main.title_bar.*


/**
 * 计算页面
 */
class CctionFragment : Fragment() {
    private val arrayList = ArrayList<ItemBean>()
    private lateinit var adapter: CommonAdapter<ItemBean>

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.cction_main, container, false)
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        initView()
    }


    private fun initView() {
        title_bar_back.visibility = View.GONE
        title_bar_other.visibility = View.GONE
        title_bar_title.text = "计算工具"

        arrayList.clear()
        arrayList.add(ItemBean(R.drawable.svg_resave, "复利"))
        arrayList.add(ItemBean(R.drawable.svg_house, "房贷"))
        arrayList.add(ItemBean(R.drawable.svg_tax, "个税"))
//        arrayList.add(ItemBean(R.drawable.svg_car, "车贷"))
//        arrayList.add(ItemBean(R.drawable.svg_card, "存款"))
//        arrayList.add(ItemBean(R.drawable.svg_exchange, "汇率"))

        cction_recycler.layoutManager = GridLayoutManager(context, 3)
        cction_recycler.addItemDecoration(GridDividerItemDecoration(context))

        adapter = object : CommonAdapter<ItemBean>(context, R.layout.item_main, arrayList) {
            override fun convert(holder: ViewHolder, t: ItemBean, position: Int) {
                holder.setImageResource(R.id.item_image, t.icon)
                holder.setText(R.id.item_name, t.name)


                holder.itemView.setOnClickListener {
                    when (t.name) {
                        "复利" -> {
                            startActivity(Intent(context, CctionNewActivity::class.java))
                        }
                        "房贷" -> {
                            startActivity(Intent(context, HouseActivity::class.java))
                        }
                        "个税" -> {
                            startActivity(Intent(context, TaxActivity::class.java))
                        }
                        "汇率" -> {
                            startActivity(Intent(context, CctionNewActivity::class.java))
                        }
                    }
                }
            }
        }
        cction_recycler.adapter = adapter
    }
}
